<?php
namespace App\Services;

use App\User;
use App\TempUser;
use App\UserPlatformOpenid;
use Carbon\Carbon;
use App\BaseDictionary;
use DB;

class VolunteerService
{
    public function checkUserByMobile($mobile)
    {
        return User::where('mobile', $mobile)->first();
    }

    public function sendBindUserVerifyCode($mobile)
    {
        DB::transaction(function () use ($mobile) {
            $user = $this->checkUserByMobile($mobile);

            $verifyCode = random_int(1000, 9999);

            if(!isset($user)) {
                $tempUser = TempUser::where('mobile', $mobile)->first();

                if(isset($tempUser)) {
                    $tempUser->mobile_verify_code = $verifyCode;
                    $tempUser->mobile_verify_begin = Carbon::now();
                    $tempUser->save();
                } else {
                    $tempUser = TempUser::create([
                        'mobile' => $mobile,
                        'mobile_verify_begin' => Carbon::now(),
                        'mobile_verify_code' => $verifyCode
                    ]);
                }
            } else {
                $user->mobile_verify_code = $verifyCode;
                $user->mobile_verify_begin = Carbon::now();
                $user->save();
            }

            $smsService = new SmsService();

            $smsService->sendMobile($mobile, config('app.name').'：您正在绑定志愿者手机号，验证码:'.$verifyCode);

        });
    }

    public function bindUserVerifyCode($mobile, $platformId, $openid, $avatar, $nickname)
    {
        DB::transaction(function () use ($mobile, $platformId, $openid, $avatar, $nickname) {
            TempUser::where('mobile', $mobile)->delete();

            $user = $this->checkUserByMobile($mobile);

            if (!isset($user)) {
                $user = User::create([
                    'mobile' => $mobile,
                    'name' => $mobile,
                    'nick_name' => $mobile,
                    'password' => random_int(100000, 999999)
                ]);
            }

            $user->is_super = BaseDictionary::$KEY_NO;
            $user->user_active = User::$USER_STATUS_ACTIVE;

            $user->volunteer_flag = User::$VOLUNTEER_FLAG_YES;

            $user->save();

            $userPlatformOpenid = UserPlatformOpenid::where('platform_id', $platformId)
                ->where('openid', $openid)->first();

            if (isset($userPlatformOpenid)) {
                UserPlatformOpenid::where('platform_id', $platformId)
                    ->where('openid', $openid)->update([
                        'user_id' => $user->id,
                        'avatar' => $avatar,
                        'nickname' => $nickname
                    ]);
            } else {
                UserPlatformOpenid::create([
                    'user_id' => $user->id,
                    'platform_id' => $platformId,
                    'openid' => $openid,
                    'avatar' => $avatar,
                    'nickname' => $nickname
                ]);
            }
        });
    }

    public function getUserInfoByOpenId($openid, $platformId)
    {
        $userPlatformOpenid = UserPlatformOpenid::where('platform_id', $platformId)
            ->where('openid', $openid)->first();

        if (isset($userPlatformOpenid)) {
            return [
                'user_id' => $userPlatformOpenid->user_id,
                'avatar' => $userPlatformOpenid->avatar,
                'nickname' => $userPlatformOpenid->nickname
            ];
        }

        return null;
    }

    public function isVerifyCodeTooFrequency($mobile)
    {
        return !$this->isVerifyCodeExpired($mobile);
    }

    public function isVerifyCodeExpired($mobile)
    {
        $user = User::where('mobile', $mobile)->first();

        if(isset($user)) {
            if( isset($user->mobile_verify_begin) )
                return Carbon::now()->diffInSeconds(Carbon::parse($user->mobile_verify_begin)) > 60 ;
        } else {
            $tempUser = TempUser::where('mobile', $mobile)->first();
            if(isset($tempUser)) {
                return Carbon::now()->diffInSeconds(Carbon::parse($tempUser->mobile_verify_begin)) > 60 ;
            }
        }

        return true;
    }

    public function isModifyVerifyCodeValid($mobile, $verifyCode)
    {
        $user = User::where('mobile', $mobile)->first();

        if(isset($user)) {
            return $user->mobile_verify_code == $verifyCode;
        } else {
            $tempUser = TempUser::where('mobile', $mobile)->first();
            if(isset($tempUser)) {
                return $tempUser->mobile_verify_code == $verifyCode;
            }
        }

        return false;
    }
}
